//
//  UCPolygon.m
//  SDK开发版本
//
//  Created by 北京国泰北斗科技有限公司 on 2016/11/15.
//  Copyright © 2016年 北京国泰北斗科技有限公司. All rights reserved.
//

#import "UCPolygon.h"
#import <GoogleMaps/GoogleMaps.h>
#import "UCMapView.h"
#import "UCMutablePath.h"
#import "UCGeometryUtils.h"

@interface UCPolygon()
{
//    int _zIndex;
//    UCMapView *_mapView;
}
@property(nonatomic,strong)GMSPolygon *polygon;
@property(nonatomic,strong)NSMutableArray *markers;
@end

@implementation UCPolygon

+ (instancetype)polygonWithPath:(UCPath *)path{
    return [[UCPolygon alloc] initPolygonWithPath:path];
}
- (instancetype)initPolygonWithPath:(UCPath *)path{

    self = [super init];
    if (self) {
        self.polygon = [GMSPolygon polygonWithPath:[path valueForKey:@"gmPath"]];
    }
    return self;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        self.polygon = [[GMSPolygon alloc] init];
    }
    return self;
}

- (void)setPath:(UCPath *)path{
    _path = path;
    self.polygon.path = [_path valueForKey:@"gmPath"];
    
    if (_showLabel) {
        if (!_markers) {
            self.markers = [[NSMutableArray alloc] init];
        }
        [self addMarker];
    }
}

- (void)setStrokeColor:(UIColor *)strokeColor{
    self.polygon.strokeColor = strokeColor;
}
- (UIColor *)strokeColor{
    return self.polygon.strokeColor;
}

- (void)setStrokeWidth:(CGFloat)strokeWidth{
    self.polygon.strokeWidth = strokeWidth;
}
- (CGFloat)strokeWidth{
    return self.polygon.strokeWidth;
}

- (void)setFillColor:(UIColor *)fillColor{
    self.polygon.fillColor = fillColor;
}
- (UIColor *)fillColor{
    return self.polygon.fillColor;
}

-(void)setGeodesic:(BOOL)geodesic{
    self.polygon.geodesic = geodesic;
}
- (BOOL)geodesic{
    return self.polygon.geodesic;
}
- (void)setZIndex:(int)zIndex{
    super.zIndex = zIndex;
    self.polygon.zIndex = zIndex;
}
- (int)zIndex{
    return self.polygon.zIndex;
}

- (void)setMapView:(UCMapView *)mapView{
    super.mapView = mapView;
    self.polygon.map = [mapView valueForKey:@"map"];
    
    if (mapView == nil && _showLabel) {
        [self removeAllMarker];
    }
}


- (void)addMarker{

    NSUInteger sumCount = self.path.count;
    CLLocationDegrees       latitude    = 0.0;
    CLLocationDegrees       longitude   = 0.0;
    CLLocationCoordinate2D  coord1;
    CLLocationCoordinate2D  coord2;
    CLLocationCoordinate2D  centerLocation;
    
    
    [self removeAllMarker];
    if (sumCount > 2) {
        for (int i = 0; i < sumCount; i++) {
            
            UCMutablePath *centerPath = [UCMutablePath path];
            
            if (i < sumCount - 1) {
                coord1 = [self.path coordinateAtIndex:i];
                coord2 = [self.path coordinateAtIndex:i+1];

                [centerPath addCoordinate:coord1];
                [centerPath addCoordinate:coord2];
            }else{
                coord1 = [self.path coordinateAtIndex:i];
                coord2 = [self.path coordinateAtIndex:0];
                
                [centerPath addCoordinate:coord1];
                [centerPath addCoordinate:coord2];
            }
            latitude = fabs((coord1.latitude - coord2.latitude)/2 + coord2.latitude);
            longitude = fabs((coord1.longitude - coord2.longitude)/2 + coord2.longitude);
            centerLocation = CLLocationCoordinate2DMake(latitude, longitude);
            
            NSString *content = [NSString stringWithFormat:@"%.2f米",CalculateTheDistanceOfTheLine(centerPath)];
            
            GMSMarker *marker = [GMSMarker markerWithPosition:centerLocation];
            if ([self.delegate respondsToSelector:@selector(showPolygonMiddleLabelWithLabelContent:)]) {
                marker.icon = [self.delegate showPolygonMiddleLabelWithLabelContent:content];
            }else{
                marker.icon = [self private_setIconImage:@{
                                                           NSFontAttributeName : [UIFont systemFontOfSize:13],
                                                           NSForegroundColorAttributeName : [UIColor whiteColor],
                                                           NSBackgroundColorAttributeName : [UIColor blackColor]
                                                           }
                                              withString:content];
            }
            marker.zIndex = super.zIndex;
            marker.map = [super.mapView valueForKey:@"map"];
            [_markers addObject:marker];
        }
    }   
}

- (void)removeAllMarker{
    for (GMSMarker *marker in _markers) {
        marker.map = nil;
    }
    [_markers removeAllObjects];
}

- (UIImage *)private_setIconImage:(NSDictionary *)dic withString:(NSString *)string{
    
    CGSize size = [string sizeWithAttributes:dic];
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
    
    //2、设置裁剪区域
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:3];
    [path addClip];
    
    [string drawInRect:rect withAttributes:dic];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}
@end
